package com.yc.login.biz;

import com.yc.common.biz.BizException;
import com.yc.common.util.Utils;
import com.yc.login.bean.LoginProperties;
import com.yc.login.dao.LoginDao;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

@Service
public class LoginBiz {

    @Resource
    private LoginDao loginDao;

    public Object login(String username, String password) throws BizException {
        Utils.checkEmpty(username, "用户名不能为空!");
        Utils.checkEmpty(password, "密码不能为空!");
        final List<?> list = loginDao.login(username, password);
        Utils.checkEmpty(list, "用户名或密码错误");
        Utils.check(list.size()>1, "系统异常,请联系管理员");
        return list.get(0);
    }

    @Resource
    private LoginProperties loginProperties;

    public void modifyPwd(Object myinfo, String password, String newPassword) throws BizException {
        // 通过反射获取原用户名和原密码
        try {
            final Class<?> aClass = Class.forName(loginProperties.getUserClass());
            // 1. 假定用户的实体类没有继承
            // 2. 假定数据库字段 == 实体类的属性, 如果不同要做转换
            final Field usernameField = aClass.getDeclaredField(loginProperties.getUsernameColumn());
            final Field passwordField = aClass.getDeclaredField(loginProperties.getPasswordColumn());
            // 强制访问
            usernameField.setAccessible(true);
            passwordField.setAccessible(true);
            String username = (String) usernameField.get(myinfo);
            String oldPassword = (String) passwordField.get(myinfo);
            Utils.checkEmpty(password,"请输入原密码!");
            Utils.checkEmpty(newPassword,"请输入新密码!");
            //Utils.checkEmpty(oldPassword.equals(password) == false,"输入的原密码错误!");
            Utils.check(oldPassword.equals(password) == false,"输入的原密码错误!");
            loginDao.updatePwd(username, password, newPassword);
            // 修改myinfo的密码
            passwordField.set(myinfo, newPassword);
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("用户类定义错误!",e);
        }
    }

    /**
     *
     * @param username
     * @param newPassword
     * @param vcode     用户输入的验证码
     * @param scode     保存在会话中的验证码
     */
    public void modifyPwd(String username, String newPassword, String vcode, String scode) throws BizException {
        Utils.checkEmpty(scode,"请先发送验证邮件!");
        Utils.checkEmpty(vcode,"请输入验证码!");
        Utils.check(scode.equals(vcode) == false, "验证码输入错误!");
        Utils.checkEmpty(newPassword,"请输入新密码!");
        loginDao.updatePwd(username, newPassword);
    }
    @Resource
    JavaMailSender mailSender;
    public String sendPwdEmail(String username) throws BizException {
        Utils.checkEmpty(username,"请输入用户名!");
        // 生成验证码
        String vcode = System.currentTimeMillis() + "";
        vcode = vcode.substring(vcode.length()-4);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("找回密码");
        simpleMailMessage.setText("找回密码的验证码: " + vcode);
        simpleMailMessage.setFrom(loginProperties.getEmailFrom());
        final String email = loginDao.selectEmailByUserame(username);
        simpleMailMessage.setTo(email);
        mailSender.send(simpleMailMessage);
        return vcode;
    }



}
